package com.cn.qtms.service.impl;//类注释设置模板

import com.cn.qtms.base.dao.BaseDao;
import com.cn.qtms.base.service.impl.BaseServiceImpl;
import com.cn.qtms.common.tree.Tree;
import com.cn.qtms.common.tree.TreeUtil;
import com.cn.qtms.dao.TmSysResourceDao;
import com.cn.qtms.dao.TmSysRoleDao;
import com.cn.qtms.entity.TmSysResource;
import com.cn.qtms.entity.TmSysRole;
import com.cn.qtms.service.TmSysRoleService;
import com.cn.qtms.entity.vo.sys.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Name:TmSysRoleServiceImpl
 * @Description:
 * @Author: zhangyang
 * @Date: 2019-11-13 21:16
 **/
@Service
public class TmSysRoleServiceImpl extends BaseServiceImpl<TmSysRole, Long> implements TmSysRoleService {
    @Autowired
    private TmSysRoleDao dao;
    @Autowired
    private TmSysResourceDao resourceDao;

    @Override
    public BaseDao<TmSysRole, Long> getDao() {
        return dao;
    }

    @Override
    public List<Tree<Resource>> findRoleResource(Set<String> roles) {
        final List<TmSysResource> roleResource = resourceDao.findRoleResource(roles);
        HashMap<String,List<Resource>> map=new HashMap<>();
        List<Tree<Resource>> trees = new ArrayList<>();
        roleResource.forEach(
                res -> {
                    Tree<Resource> tree = new Tree<>();
                    tree.setId(res.getId().toString());
                    tree.setParentId(res.getParentId().toString());
                    tree.setTitle(res.getResName());
                    Map<String, Object> attributes = new HashMap<>(16);
                    attributes.put("permissions",roles );
                    tree.setMeta(attributes);
                    tree.setIsLeaf(res.getIsLeaf());
                    tree.setResType(res.getResType());
                    tree.setPath(res.getResPath());
                    tree.setIcon(res.getIcon());
                    tree.setName(res.getResName());
                    tree.setExact(true);
                    tree.setRescode(res.getResCode());
                    trees.add(tree);
                }
        );
        List<Tree<Resource>> list = TreeUtil.buildList(trees, "0");

        return list;
    }

    @Override
    public List<String> findRolePermission(Set<String> roles) {
        return resourceDao.findPermissionList(roles);
    }
}
